package com.ruoyi.web.controller.fund;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.fileDetail.domain.HouseFileDetail;
import com.ruoyi.fileDetail.service.IHouseFileDetailService;
import com.ruoyi.filingCabinet.domain.FilingCabinet;
import com.ruoyi.filingCabinet.service.IFilingCabinetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 档案柜Controller
 * 
 * @author lijie
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/fund/filingCabinet")
public class FilingCabinetController extends BaseController
{
    @Autowired
    private IFilingCabinetService filingCabinetService;

    @Autowired
    private IHouseFileDetailService houseFileDetailService;

    /**
     * 查询档案柜列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FilingCabinet filingCabinet)
    {
        startPage();
        List<FilingCabinet> list = filingCabinetService.selectFilingCabinetList(filingCabinet);
        for(FilingCabinet filingCabinet1:list){
            if(filingCabinet1.getFilingDescription()==null){
                filingCabinet1.setFilingDescription("");
            }
            if(filingCabinet1.getFilingDescription()==null){
                filingCabinet1.setFilingDescription("");
            }
            if(filingCabinet1.getFilingDescription().length()>5){
                filingCabinet1.setFilingDescription(filingCabinet1.getFilingDescription().substring(0,5)+"...");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出档案柜列表
     */
    @Log(title = "档案柜", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FilingCabinet filingCabinet)
    {
        List<FilingCabinet> list = filingCabinetService.selectFilingCabinetList(filingCabinet);
        ExcelUtil<FilingCabinet> util = new ExcelUtil<FilingCabinet>(FilingCabinet.class);
        util.exportExcel(response, list, "档案柜数据");
    }

    /**
     * 获取档案柜详细信息
     */
    @GetMapping(value = "/{filingId}")
    public AjaxResult getInfo(@PathVariable("filingId") Long filingId)
    {
        return AjaxResult.success(filingCabinetService.selectFilingCabinetByFilingId(filingId));
    }

    /**
     * 新增档案柜
     */
    @Log(title = "档案柜", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FilingCabinet filingCabinet)
    {
        filingCabinet.setCreater(getUsername());
        return toAjax(filingCabinetService.insertFilingCabinet(filingCabinet));
    }

    /**
     * 修改档案柜
     */
    @Log(title = "档案柜", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FilingCabinet filingCabinet)
    {
        return toAjax(filingCabinetService.updateFilingCabinet(filingCabinet));
    }

    /**
     * 删除档案柜
     */
    @Log(title = "档案柜", businessType = BusinessType.DELETE)
	@DeleteMapping("/{filingId}")
    public AjaxResult remove(@PathVariable Long filingId) throws Exception {
        HouseFileDetail houseFileDetail = new HouseFileDetail();
        houseFileDetail.setFilingId(filingId);
        List<HouseFileDetail> list = houseFileDetailService.selectHouseFileDetailList(houseFileDetail);
        if(list.size()>0){
            throw new Exception("当前档案柜中存在档案不允许删除！");
        }else{
            filingCabinetService.deleteFilingCabinetByFilingId(filingId);
        }
        return toAjax(1);
    }
}
